package com.xy.shop.web.service.impl;

import com.xy.shop.common.constant.CommonContants;
import com.xy.shop.common.constant.Rst;
import com.xy.shop.common.utils.DateUtil;
import com.xy.shop.common.utils.ExcelImportUtil;
import com.xy.shop.web.dao.ImportOrderMapper;
import com.xy.shop.web.dto.ImportOrder;
import com.xy.shop.web.service.ImportOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@Description: 订单导入对象
*@Param:  importOrderMapper
*@Author: zyy
*@date: 2importOrderMapper2importOrderMapper/6/24
*@Time: 14:27
*/
@Service
public class ImportOrderServiceImpl implements ImportOrderService {

    @Resource
    private ImportOrderMapper importOrderMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return importOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ImportOrder record) {
        return importOrderMapper.insert(record);
    }

    @Override
    public int insertSelective(ImportOrder record) {
        return importOrderMapper.insertSelective(record);
    }

    @Override
    public ImportOrder selectByPrimaryKey(Integer id) {
        return importOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ImportOrder record) {
        return importOrderMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ImportOrder record) {
        return importOrderMapper.updateByPrimaryKey(record);
    }

    @Override
    public Map<String, Object> onekeyImportOrder(MultipartFile file) {
        List<List<Object>> beansByExcel = null;

        try {
            beansByExcel = ExcelImportUtil.getBeansByExcel(file.getInputStream(), file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<ImportOrder>  importOrderList=new ArrayList<ImportOrder>();
        if (beansByExcel ==null ){
            return Rst.getReturnData(null, false, CommonContants.FAILURE, "插入失败，无数据！");
        }
        for (int i = 0; i < beansByExcel.size(); i++) {
            List<Object> listBean=beansByExcel.get( i);
            ImportOrder sgRecord=new ImportOrder();
            if(null == listBean || listBean.size() ==0 ){ continue; }
            if(StringUtils.isBlank(listBean.get(0).toString())) { continue; }
            if (listBean.get(8).toString().equals("")){continue; }
            if (listBean.get(9).toString().equals("")){continue; }
            sgRecord.setAddressee(listBean.get(0).toString());
            sgRecord.setStoreId(Long.valueOf(listBean.get(1).toString().replace("'","")));
            sgRecord.setOrderId(listBean.get(2).toString().replace("'",""));
            sgRecord.setOrderTime(DateUtil.StrToDateMM(getTime(listBean.get(3).toString())));
            sgRecord.setOrderStatus(listBean.get(4).toString());
            sgRecord.setGoodsName(listBean.get(5).toString());
            sgRecord.setSpecifications(listBean.get(6).toString());
            sgRecord.setPrice( new BigDecimal (listBean.get(7).toString()));
            sgRecord.setNum(Integer.valueOf(listBean.get(8).toString()));
            sgRecord.setSubtotal(listBean.get(9).toString());
            sgRecord.setGoodsId(Long.valueOf(listBean.get(10).toString()));
            sgRecord.setSku(listBean.get(11).toString());
            sgRecord.setStoreId(Long.decode(listBean.get(12).toString()));
            sgRecord.setAddresseeName(listBean.get(13).toString());
            sgRecord.setAddresseePhone(listBean.get(14).toString());

            /**买家id*/
            sgRecord.setBuyerId(listBean.get(15).toString());

            sgRecord.setProvince(listBean.get(16).toString());
            sgRecord.setCity(listBean.get(17).toString());
            sgRecord.setArea(listBean.get(18).toString());
            sgRecord.setStreet(listBean.get(19).toString());

           /* sgRecord.setLogisticsCompany(listBean.get(20).toString());
            sgRecord.setExpressNumber(listBean.get(21).toString());*/
            importOrderList.add(sgRecord);
        }

        if (importOrderList ==null || importOrderList.size() <=0 ){
            return Rst.getReturnData(null, false, CommonContants.FAILURE, "暂无数据更新！");
        }
        int count = importOrderMapper.insertBatchOrderList(importOrderList);
        if (count > 0) {
            return Rst.getReturnData(null, true, CommonContants.FAILURE_SUCCESS, "导入成功！");
        } else {
            return Rst.getReturnData(null, true, CommonContants.FAILURE_SUCCESS, "导入失败！");
        }
    }

    public static String getTime(String time){
        time = time.replace("/","-");
        time = time.replace("上午","");
        time = time.replace("下午","");
        return time;
    }
}
